package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.LqzrdjlbBaDetail;
import com.ruoyi.report_lq.domain.ReportLqBmjrsyjcjlbBa3;
import com.ruoyi.report_lq.domain.vo.LqzrdjlbBaDetailVo;
import com.ruoyi.report_lq.domain.vo.ReportLqBmjrsyjcjlbBa3Vo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqCcwdxBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqCcwdxBaDetailVo;
import com.ruoyi.report_lq.domain.ReportRhlqCcwdxBaDetail;
import com.ruoyi.report_lq.mapper.ReportRhlqCcwdxBaDetailMapper;
import com.ruoyi.report_lq.service.IReportRhlqCcwdxBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-06-28
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqCcwdxBaDetailServiceImpl implements IReportRhlqCcwdxBaDetailService {

    private final ReportRhlqCcwdxBaDetailMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ReportRhlqCcwdxBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }


    /**
     *
     * @param infoId
     * @return 存储稳定性集合
     * @method 根据info-id 查询存储稳定性度子表的list集合数据
     */
    public List<ReportRhlqCcwdxBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportRhlqCcwdxBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportRhlqCcwdxBaDetailVo> queryPageList(ReportRhlqCcwdxBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqCcwdxBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqCcwdxBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportRhlqCcwdxBaDetailVo> queryList(ReportRhlqCcwdxBaDetailBo bo) {
        LambdaQueryWrapper<ReportRhlqCcwdxBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqCcwdxBaDetail> buildQueryWrapper(ReportRhlqCcwdxBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqCcwdxBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportRhlqCcwdxBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(bo.getRongqjblbzlA() != null, ReportRhlqCcwdxBaDetail::getRongqjblbzlA, bo.getRongqjblbzlA());
        lqw.eq(bo.getRongqjblbzlC() != null, ReportRhlqCcwdxBaDetail::getRongqjblbzlC, bo.getRongqjblbzlC());
        lqw.eq(bo.getRongqblbsyzlB() != null, ReportRhlqCcwdxBaDetail::getRongqblbsyzlB, bo.getRongqblbsyzlB());
        lqw.eq(bo.getRongqblbsyzlD() != null, ReportRhlqCcwdxBaDetail::getRongqblbsyzlD, bo.getRongqblbsyzlD());
        lqw.eq(bo.getShiyzlE() != null, ReportRhlqCcwdxBaDetail::getShiyzlE, bo.getShiyzlE());
        lqw.eq(bo.getShiyzlF() != null, ReportRhlqCcwdxBaDetail::getShiyzlF, bo.getShiyzlF());
        lqw.eq(bo.getRongqblbclwzlG() != null, ReportRhlqCcwdxBaDetail::getRongqblbclwzlG, bo.getRongqblbclwzlG());
        lqw.eq(bo.getRongqblbclwzlH() != null, ReportRhlqCcwdxBaDetail::getRongqblbclwzlH, bo.getRongqblbclwzlH());
        lqw.eq(bo.getCanlwzlM() != null, ReportRhlqCcwdxBaDetail::getCanlwzlM, bo.getCanlwzlM());
        lqw.eq(bo.getCanlwzlN() != null, ReportRhlqCcwdxBaDetail::getCanlwzlN, bo.getCanlwzlN());
        lqw.eq(bo.getCanlwhlJ() != null, ReportRhlqCcwdxBaDetail::getCanlwhlJ, bo.getCanlwhlJ());
        lqw.eq(bo.getCanlwhlK() != null, ReportRhlqCcwdxBaDetail::getCanlwhlK, bo.getCanlwhlK());
        lqw.eq(bo.getChucwdbvxcz() != null, ReportRhlqCcwdxBaDetail::getChucwdbvxcz, bo.getChucwdbvxcz());
        lqw.eq(bo.getChucwdx() != null, ReportRhlqCcwdxBaDetail::getChucwdx, bo.getChucwdx());
        lqw.eq(bo.getDiyt() != null, ReportRhlqCcwdxBaDetail::getDiyt, bo.getDiyt());
        lqw.eq(bo.getDiet() != null, ReportRhlqCcwdxBaDetail::getDiet, bo.getDiet());
        lqw.eq(bo.getDist() != null, ReportRhlqCcwdxBaDetail::getDist, bo.getDist());
        lqw.eq(bo.getDisit() != null, ReportRhlqCcwdxBaDetail::getDisit, bo.getDisit());
        lqw.eq(bo.getDiwt() != null, ReportRhlqCcwdxBaDetail::getDiwt, bo.getDiwt());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportRhlqCcwdxBaDetailBo bo) {
        ReportRhlqCcwdxBaDetail add = BeanUtil.toBean(bo, ReportRhlqCcwdxBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportRhlqCcwdxBaDetailBo bo) {
        ReportRhlqCcwdxBaDetail update = BeanUtil.toBean(bo, ReportRhlqCcwdxBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量新增/修改存储稳定性记录子表3
     */
    public Boolean insertOrUpdateByCcBo(List<ReportRhlqCcwdxBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportRhlqCcwdxBaDetail detail = new ReportRhlqCcwdxBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setCommonId(pId);
            detail.setChucwdx(detailVo.getChucwdx());
            detail.setChucwdbvxcz(detailVo.getChucwdbvxcz());
            detail.setRongqjblbzlA(detailVo.getRongqjblbzlA());
            detail.setRongqblbsyzlB(detailVo.getRongqblbsyzlB());
            detail.setRongqjblbzlC(detailVo.getRongqjblbzlC());
            detail.setRongqblbsyzlD(detailVo.getRongqblbsyzlD());
            detail.setShiyzlE(detailVo.getShiyzlE());
            detail.setShiyzlF(detailVo.getShiyzlF());
            detail.setRongqblbclwzlG(detailVo.getRongqblbclwzlG());
            detail.setRongqblbclwzlH(detailVo.getRongqblbclwzlH());
            detail.setCanlwhlJ(detailVo.getCanlwhlJ());
            detail.setCanlwhlK(detailVo.getCanlwhlK());
            detail.setCanlwzlM(detailVo.getCanlwzlM());
            detail.setCanlwzlN(detailVo.getCanlwzlN());

            detail.setDiyt(detailVo.getDiyt());
            detail.setDiet(detailVo.getDiet());
            detail.setDist(detailVo.getDist());
            detail.setDisit(detailVo.getDisit());
            detail.setDiwt(detailVo.getDiwt());

            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqCcwdxBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
